package EX3;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class demo7  extends JFrame implements ActionListener{

    private JPanel p;
    private JLabel lblNmae,lblPwd;
    private JTextField txtName;
    private JPasswordField txtPwd;
    private JButton b1,b2;

    public demo7(){
        super("用户注册与登录");
        p=new JPanel(null);
        lblNmae=new JLabel("用户名");
        lblPwd=new JLabel("密 码");
        txtName=new JTextField(20);
        txtPwd=new JPasswordField(20);
        b1=new JButton("注册");
        b2=new JButton("登录");
        b1.addActionListener(this);
        b2.addActionListener(this);

        lblNmae.setBounds(30,30,60,25);
        txtName.setBounds(95,30,120,25);
        lblPwd.setBounds(30,60,60,25);
        txtPwd.setBounds(95,60,120,25);
        b1.setBounds(30,100,60,25);
        b2.setBounds(150,100,60,25);

        p.add(lblNmae);
        p.add(lblPwd);
        p.add(txtName);
        p.add(txtPwd);
        p.add(b1);
        p.add(b2);
        this.add(p);

        this.setBounds(200,100,280,220);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        MysqlDataSource ds=new MysqlDataSource();
        ds.setUrl("jdbc:mysql://localhost:3306/school");
        ds.setUser("root");
        ds.setPassword("123456");
        try {
            Connection conn=ds.getConnection();
            Statement stmt= conn.createStatement();
            String name=txtName.getText();
            String pwd=txtPwd.getText();
            //b1
            if (e.getSource()==b1){
                ResultSet rs= stmt.executeQuery("select ID,PASSWORD from user where ID='"+name+"'");
                if (rs.next()){
                    JOptionPane.showMessageDialog(p,"该用户已存在！");
                    return;
                }else {
                    stmt.executeUpdate("insert into user values('"+name+"','"+pwd+"')");
                    JOptionPane.showMessageDialog(p,"注册成功，该用户已保存！");
                    txtName.setText("");
                    txtPwd.setText("");
                }
            }

            //b2
            if (e.getSource()==b2){
                ResultSet rs = stmt.executeQuery("select ID,PASSWORD from user where ID='" + name + "' and PASSWORD='"+pwd+"'");
                if (rs.next()){
                    JOptionPane.showMessageDialog(p,"登陆成功");
                }else {
                    JOptionPane.showMessageDialog(p,"登陆失败，请核对用户密码");
                }

                rs.close();
                stmt.close();
                conn.close();
            }
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }

    }

    public static void main(String[] args) {
        new demo7();
    }
}
